package com.yanqu.road.logic.activity.silkroad.refloat;

import java.util.List;

//鱼钩,形状是球形,x和y指定中心点坐标，有半径，有速度，有发射角度
public class SilkRoadHook {
    private double x;
    private double y;
    private double radius;
    private double speed;
    private double angle;
    private double angleSpeed;
    private int direction; //1增加,2减少
    private boolean isReturn = false; //是否回勾
    private double returnSpeed; //回勾速度
    private double maxAngle;
    private double minAngle;
    private double returnX = -1;
    private double returnY = -1;


    public SilkRoadHook(double radius, double speed, double angleSpeed, double maxAngleChange) {
        this.radius = radius;
        this.speed = speed;
        this.angle = 0;//垂直向下是0°
        this.angleSpeed = angleSpeed;
        this.maxAngle = angle + maxAngleChange;
        this.minAngle = angle - maxAngleChange;
        this.direction = 1;
        this.returnSpeed = speed * 2;
    }

    public SilkRoadHook(List<Integer> hookParams) {
        this.radius = hookParams.get(4);
        this.speed = hookParams.get(2);
        this.angle = 0;//垂直向下是0°
        this.angleSpeed = hookParams.get(1);
        this.maxAngle = angle + hookParams.get(0);
        this.minAngle = angle - hookParams.get(0);
        this.direction = 1;
        this.returnSpeed = hookParams.get(3);
    }

    public void move() {
        double sin = Math.sin(Math.PI / (180 / angle));
        double cos = Math.cos(Math.PI / (180 / Math.abs(angle)));


        if (!isReturn) {
            x -= getMoveSpeed() * sin;
            y -= getMoveSpeed() * cos;
        } else {
            x += getMoveSpeed() * sin;
            y += getMoveSpeed() * cos;
        }

//        if (y <= 0) {
//            isReturn = true;
//            speed = returnSpeed;
//            y = -y;
//
//            returnX=x;
//            returnY=y;
//        }
//
//        if (x <= 0) {
//            isReturn = true;
//            speed = returnSpeed;
//            x = -x;
//
//            returnX=x;
//            returnY=y;
//        }
//
//        if (x >= SilkRoadSeaScence.maxWidth) {
//            isReturn = true;
//            speed = returnSpeed;
//            x = SilkRoadSeaScence.maxWidth - (x - SilkRoadSeaScence.maxWidth);
//
//            returnX=x;
//            returnY=y;
//        }
    }

    /**
     * speed的单位是秒,移动按100ms算一次
     *
     * @return
     */
    public double getMoveSpeed() {
        return speed / 10;
    }

    //该方法每调用一次就会修改10°角度,达到60°后就反向递减到-60°然后再反向递增到60°,如此循环
    public void changeAngle() {
        if (direction == 1) {
            angle += angleSpeed;
            if (angle >= maxAngle) {
                angle = maxAngle - (angle - maxAngle);
                direction = 2;
            }
        } else if (direction == 2) {
            angle -= angleSpeed;
            if (angle <= minAngle) {
                angle = minAngle + (minAngle - angle);
                direction = 1;
            }
        }
    }

    public void changeAngleByTime(long time) {
        if (direction == 1) {
            angle += angleSpeed * time / 1000;
        } else if (direction == 2) {
            angle -= angleSpeed * time / 1000;
        }

        while (angle < minAngle || angle > maxAngle) {
            if (angle < minAngle) {
                angle = minAngle + (minAngle - angle);
                direction = 1;
            } else if (angle > maxAngle) {
                angle = maxAngle - (angle - maxAngle);
                direction = 2;
            }
        }
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double getRadius() {
        return radius;
    }

    public double getAngle() {
        return angle;
    }

    public int getDirection() {
        return direction;
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void initMove(int initMove) {
        double sin = Math.sin(Math.PI / (180 / angle));
        x -= initMove * sin;
        double cos = Math.cos(Math.PI / (180 / Math.abs(angle)));
        y -= initMove * cos;
    }

    public double getReturnX() {
        return returnX;
    }

    public double getReturnY() {
        return returnY;
    }
}
